//
//  BaseTabBarViewController.m
//  魔兽大数据
//
//  Created by 王右 on 2017/10/24.
//  Copyright © 2017年 王右. All rights reserved.
//

#import "BaseTabBarViewController.h"
#import "BaseNavigationViewController.h"
#import "MineViewController.h"
#import "PopulationCensusViewController.h"
#import "ServerListViewController.h"
#import "BigDataHeader.h"
#import "MineHomeViewController.h"

@protocol tDelegate <NSObject>

- (void)ttabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;

@end

@interface BaseTabControllerDelegateTool : NSObject<UITabBarControllerDelegate>

@property (nonatomic, weak)id<tDelegate> delegate;

- (instancetype)initWithVC:(UITabBarController *)tabVC;

@end

@implementation BaseTabControllerDelegateTool

- (instancetype)initWithVC:(UITabBarController *)tabVC{
    if (self = [super init]) {
        tabVC.delegate = self;
    }
    return self;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    [self.delegate ttabBarController:tabBarController didSelectViewController:viewController];
}

@end

@interface BaseTabBarViewController ()<tDelegate>{
    BaseTabControllerDelegateTool *tool;
}
@property (nonatomic, assign) NSInteger currentIndex;

@end



@implementation BaseTabBarViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    _currentIndex = 0;

    PopulationCensusViewController *vc1 = [PopulationCensusViewController new];
    vc1.title = @"人口普查";
    BaseNavigationViewController *nav1 = [[BaseNavigationViewController alloc] initWithRootViewController:vc1];

    
    UIViewController *vc2 =  [[UIViewController alloc]init];
    vc2.view.backgroundColor = [UIColor blueColor];
    vc2.title = @"拍卖";
    BaseNavigationViewController *nav2 = [[BaseNavigationViewController alloc]initWithRootViewController:vc2];
    

    ServerListViewController *vc3 = [ServerListViewController new];
    vc3.view.backgroundColor = [UIColor grayColor];
    vc3.title = @"黑市";
    BaseNavigationViewController *nav3 = [[BaseNavigationViewController alloc] initWithRootViewController:vc3];
    

    MineHomeViewController *vc4 = [MineHomeViewController new];
    vc4.title = @"我的";
    BaseNavigationViewController *nav4 = [[BaseNavigationViewController alloc] initWithRootViewController:vc4];

    self.viewControllers = @[nav1,nav2,nav3,nav4];
    
    
    NSArray *titleArray = @[@"人口普查",@"拍卖",@"黑市",@"我的"];
    NSArray *imageArray = @[@"数据",@"金币",@"搜索",@"个人中心"];
    NSInteger index = 0;
    for (UITabBarItem *item in self.tabBar.items) {
        item.title = titleArray[index];
        
        NSString *imageString = [NSString stringWithFormat:@"%@_unselect",imageArray[index]];
        NSString *imageString2 = [NSString stringWithFormat:@"%@_select",imageArray[index]];
        
        item.image = [[UIImage imageNamed:imageString] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        item.selectedImage = [[UIImage imageNamed:imageString2] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [item setImageInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        item.titlePositionAdjustment = UIOffsetMake(0, 0);
        index ++;
    }
    
    
    
    //Tab设置
    UITabBarItem *tabBarItem = [UITabBarItem appearance];
    [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName :RGB(136, 136, 136)}
                              forState:UIControlStateNormal];
    [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName :RGB(234, 85, 4)}
                              forState:UIControlStateSelected];
    
    
    //Tab设置
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    self.tabBar.translucent = NO;
    self.selectedIndex = 0;
    
    //去掉tabBar顶部线条
    
//    if ([UIDevice currentDevice].systemVersion.floatValue <9.0) {
//        CGRect rect = CGRectMake(0, 0, 1, 1);
//        UIGraphicsBeginImageContext(rect.size);
//        CGContextRef context = UIGraphicsGetCurrentContext();
//        CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
//        CGContextFillRect(context, rect);
//        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
//        UIGraphicsEndImageContext();
//        [self.tabBar setBackgroundImage:img];
//        [self.tabBar setShadowImage:img];
//    }
    
    
    tool = [[BaseTabControllerDelegateTool alloc]initWithVC:self];
    tool.delegate = self;
}

- (void)ttabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    NSInteger index = self.selectedIndex;
//    BaseNavigationViewController *nav1 = tabBarController.viewControllers[0];
//    PopulationCensusViewController *vc1 = nav1.viewControllers[0];
    switch (index) {
        case 0:
//            [vc1 getData];
            break;
            
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
